//
//  OrderDetailViewController.m
//  OrderFood
//
//  Created by xiaofang wang on 2018/10/25.
//  Copyright © 2018年 zhiyuan. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "AbnomalDetailViewController.h"
#import "OrderDetailView.h"
#import "SureSendView.h"
#import "MapView.h"
@interface OrderDetailViewController () < SureSendViewDelegate >
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) UIButton *leftActionBtn;
@property (nonatomic, strong) UIButton *rightActionBtn;
@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.name = @"订单详情";
    self.view.backgroundColor = BACKGROUNDCOLOR;
    [self setUI];
}

-(void)setUI{
    //父试图
    
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SafeAreaTopHeight-80 - SafeAreaBottomHeight)]; ////iphoneX 底部适配
    self.contentScrollView.backgroundColor = BACKGROUNDCOLOR;
    [self.view addSubview:self.contentScrollView];
    //头部试图
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
    topView.backgroundColor = WHITECOLOR;
    [self.contentScrollView addSubview:topView];
    [QTTools tool_addLineWithFrame:CGRectMake(SCREEN_WIDTH/2, 10, 1, topView.mh_height-20) superView:topView];
    NSArray *titleAry = @[NSLocalizedString(@"bendanshouru", nil),NSLocalizedString(@"qiwangsongdashijian", nil)];
    NSArray *dataAry = @[[NSString stringWithFormat:@"¥ %@", _orderModel.deliveryCommission],@"12:25"];
    for (int i = 0; i < 2; i++) {
        UILabel *dataLb = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2*i, 20, SCREEN_WIDTH/2, 20)];
        dataLb.font = FONT(24);
        dataLb.textColor = BLACKCOLOR;
        dataLb.textAlignment = NSTextAlignmentCenter;
        dataLb.text = dataAry[i];
        [topView addSubview:dataLb];
        if (i == 0) {
            dataLb.textColor = themeColor;
        }
        
        UILabel *bottomLb = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2*i, dataLb.mh_bottom+15, SCREEN_WIDTH/2, 15)];
        bottomLb.font = FONT(15);
        bottomLb.textColor = themeTextColor;
        bottomLb.textAlignment = NSTextAlignmentCenter;
        bottomLb.text = titleAry[i];
        [topView addSubview:bottomLb];
    }
    
    //地图(谷歌)
    UIView *mapBgView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.mh_bottom, SCREEN_WIDTH, 310)];
    [self.contentScrollView addSubview:mapBgView];
    UILabel *routeLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 35)];
    routeLb.font = FONT(13);
    routeLb.textColor = themeTextColor;
    routeLb.text = NSLocalizedString(@"route", nil);
    [mapBgView addSubview:routeLb];

    CLLocationCoordinate2D storePosition = CLLocationCoordinate2DMake([_orderModel.shop.latitude doubleValue], [_orderModel.shop.longitude doubleValue]);
    CLLocationCoordinate2D userPosition = CLLocationCoordinate2DMake([_orderModel.user.latitude doubleValue], [_orderModel.user.longitude doubleValue]);

    MapView *mapView = [[MapView alloc] initWithFrame:CGRectMake(0, routeLb.mh_bottom, SCREEN_WIDTH, 275) storePosition:storePosition userPosition:userPosition orderState:self.orderState];
    mapView.backgroundColor = WHITECOLOR;
    [mapBgView addSubview:mapView];
    
    
    //订单配送
    UIView *orderSendBgView = [[UIView alloc] initWithFrame:CGRectMake(0, mapBgView.mh_bottom, SCREEN_WIDTH, 225)];
    [self.contentScrollView addSubview:orderSendBgView];
    UILabel *sendLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 35)];
    sendLb.font = FONT(13);
    sendLb.textColor = themeTextColor;
    sendLb.text = NSLocalizedString(@"detail_orderSend", nil);
    [orderSendBgView addSubview:sendLb];
    OrderDetailView *detailView = [[OrderDetailView alloc] initWithFrame:CGRectMake(0, sendLb.mh_bottom, SCREEN_WIDTH, 190)];
    detailView.orderState = self.orderState;
    detailView.backgroundColor = WHITECOLOR;
    detailView.orderModel = _orderModel;
    [orderSendBgView addSubview:detailView];
    [detailView.storeCallBtn addTarget:self action:@selector(callMerchant) forControlEvents:UIControlEventTouchUpInside];
    [detailView.contactCallBtn addTarget:self action:@selector(callUser) forControlEvents:UIControlEventTouchUpInside];
    //底部备注，订单号
    UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(0, orderSendBgView.mh_bottom, SCREEN_WIDTH, 110)];
    [self.contentScrollView addSubview:otherView];
    
    UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 100)];
    markView.backgroundColor = WHITECOLOR;
    [otherView addSubview:markView];
    UILabel *markLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 50)];
    markLb.font = FONT(14);
    markLb.numberOfLines = 0;
    
    NSString *markStr = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"mark", nil),_orderModel.remark];
    if (_orderModel.remark.length == 0 || _orderModel.remark == nil) {
        markStr =  [NSString stringWithFormat:@"%@",NSLocalizedString(@"No remark", nil)];
    }
    markLb.text = markStr;
    [markView addSubview:markLb];
    CGSize markSize = [QTTools fc_getStringSizeWith:markStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-30, 500) font:FONT(14)];
    if (markSize.height > 50) {
        markLb.frame = CGRectMake(15, 0, SCREEN_WIDTH-30, markSize.height+15);
        markView.frame = CGRectMake(0, 10, SCREEN_WIDTH, 50+markLb.mh_height);
        otherView.frame = CGRectMake(0, orderSendBgView.mh_bottom, SCREEN_WIDTH, 60+markLb.mh_height);
    }else if (markSize.height >40 && markSize.height <50) {
        markLb.frame = CGRectMake(15, 0, SCREEN_WIDTH-30, 50+15);
        markView.frame = CGRectMake(0, 10, SCREEN_WIDTH, 50+markLb.mh_height);
        otherView.frame = CGRectMake(0, orderSendBgView.mh_bottom, SCREEN_WIDTH, 60+markLb.mh_height);
    }
    
//    markView.backgroundColor = [UIColor redColor];
//    markLb.backgroundColor = [UIColor orangeColor];
    
    [QTTools tool_addLineWithFrame:CGRectMake(0, GetMaxY(markLb), SCREEN_WIDTH, 1) superView:markView];
    
    UILabel *orderNumLb = [[UILabel alloc] initWithFrame:CGRectMake(15, markLb.mh_bottom, SCREEN_WIDTH-30, 50)];
    orderNumLb.font = FONT(14);
    orderNumLb.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"orderNum", nil),_orderModel.orderId];
    [markView addSubview:orderNumLb];
    self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, otherView.mh_bottom);
    
    

    //12*-29底部bottomView
    UIView *bottomBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentScrollView.mh_bottom+10, SCREEN_WIDTH, 40)];
    [self.view addSubview:bottomBgView];
    
    UIButton *leftActionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    leftActionBtn.frame = CGRectMake(30, SCREEN_HEIGHT-SafeAreaTopHeight-60, 120, 40);
    leftActionBtn.frame = CGRectMake(30, 0, 120, 40);
    leftActionBtn.backgroundColor = WHITECOLOR;
    leftActionBtn.titleLabel.font = FONT(15);
    leftActionBtn.layer.cornerRadius = 5;
    [leftActionBtn setTitle:NSLocalizedString(@"detail_ycsb", nil) forState:UIControlStateNormal];
    [leftActionBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
//    [self.view addSubview:leftActionBtn];
    [bottomBgView addSubview:leftActionBtn];
    
    self.leftActionBtn = leftActionBtn;
    if (_orderState == ReceiveState) {
        self.leftActionBtn.hidden = YES;
    }
    UIButton *rightActionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    rightActionBtn.frame = CGRectMake(SCREEN_WIDTH-150, SCREEN_HEIGHT-SafeAreaTopHeight-60, 120, 40);
    rightActionBtn.frame = CGRectMake(SCREEN_WIDTH-150, 0, 120, 40);
    rightActionBtn.backgroundColor = WHITECOLOR;
    rightActionBtn.titleLabel.font = FONT(15);
    rightActionBtn.layer.cornerRadius = 5;
    [rightActionBtn setTitle:NSLocalizedString(@"detail_jd", nil) forState:UIControlStateNormal];
    [rightActionBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
//    [self.view addSubview:rightActionBtn];
    [bottomBgView addSubview:rightActionBtn];
    self.rightActionBtn = rightActionBtn;
    [self.leftActionBtn addTarget:self action:@selector(actionOfLeft) forControlEvents:UIControlEventTouchUpInside];
    [self.rightActionBtn addTarget:self action:@selector(actionOfRight) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.orderState == ReceiveState) {
        [rightActionBtn setTitle:NSLocalizedString(@"detail_jd", nil) forState:UIControlStateNormal];
    }else if (self.orderState == GetFoodState){
        [rightActionBtn setTitle:NSLocalizedString(@"detail_getFood", nil) forState:UIControlStateNormal];
    }else if (self.orderState == SendState){
        [rightActionBtn setTitle:NSLocalizedString(@"detail_send", nil) forState:UIControlStateNormal];
    }
    
    
   
    
}

#pragma mark - 拨打商家电话
-(void)callMerchant{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_callMerchant", nil) message:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"detail_call", nil),_orderModel.shop.phone] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

#pragma mark - 拨打用户电话
-(void)callUser{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_callUser", nil) message:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"detail_call", nil),_orderModel.user.phone] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}


-(void)actionOfLeft {
    AbnomalDetailViewController *abDetailVC = [[AbnomalDetailViewController alloc] init];
    abDetailVC.orderModel = _orderModel;
    [self.navigationController pushViewController:abDetailVC animated:YES];
}

-(void)actionOfRight{
    if (self.orderState == ReceiveState) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_sureRecieve", nil) message:NSLocalizedString(@"detail_whethersureRecieve", nil) preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        WeakObj(self)
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakself netWorkGetOrder];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else if (self.orderState == GetFoodState){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_suregetFood", nil) message:NSLocalizedString(@"detail_whethersuregetFood", nil) preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        WeakObj(self)
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakself netWorkGetFood];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else if (self.orderState == SendState){
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_suresend", nil) message:NSLocalizedString(@"detail_whethersuresend", nil) preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        WeakObj(self)
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakself netWorkFinshSendFood];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil]; 
    }
}



#pragma mark - 接单网络操作
-(void)netWorkGetOrder {
//    [HUD showMessage:@"操作成功"];
//    [self.navigationController popViewControllerAnimated:YES];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"selectindex" object:@{@"index":@(1)}];
//    return;
    [HUD showOperateBegin:@""];
    WeakObj(self)
    [NetWork netWorkGetOrder:_orderModel.orderId success:^(NSDictionary * _Nonnull response) {
        [HUD showOperateEnd];
        if (RES_SUCESS) {
            [HUD showMessage:@"操作成功"];
            [weakself.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"selectindex" object:@{@"index":@(1)}];
        }else {
            [HUD showMessage:MESSAGE];
        }
    } failure:^(NSString * _Nonnull message) {
        [HUD showOperateEnd];
    }];
}

#pragma mark - 确认取餐网络操作
-(void)netWorkGetFood {
//    [HUD showMessage:@"操作成功"];
//    [self.navigationController popViewControllerAnimated:YES];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"selectindex" object:@{@"index":@(2)}];
//
//    return;
    
    [HUD showOperateBegin:@""];
    WeakObj(self)
    [NetWork netWorkGetFood:_orderModel.orderId success:^(NSDictionary * _Nonnull response) {
        [HUD showOperateEnd];
        if (RES_SUCESS) {
            
            [HUD showMessage:@"操作成功"];
            [weakself.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"selectindex" object:@{@"index":@(2)}];
            
        }else {
            [HUD showMessage:MESSAGE];
        }
    } failure:^(NSString * _Nonnull message) {
        [HUD showOperateEnd];
    }];
}

#pragma mark - 确认送达网络操作
-(void)netWorkFinshSendFood {
//    [self finishOrder];
//    return;
    [HUD showOperateBegin:@""];
    WeakObj(self)
    [NetWork netWorkSendFinish:_orderModel.orderId success:^(NSDictionary * _Nonnull response) {
        [HUD showOperateEnd];
        if (RES_SUCESS) {
            [HUD showMessage:@"操作成功"];
            [weakself finishOrder];
        }else {
            [HUD showMessage:MESSAGE];
        }
    } failure:^(NSString * _Nonnull message) {
        [HUD showOperateEnd];
    }];
}




-(void)finishOrder{
    SureSendView *sureView = [[SureSendView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) withTitle:NSLocalizedString(@"detail_orderFinish",nil)];
    sureView.delegateS = self;
    [self.navigationController.view addSubview:sureView];
}


#pragma mark - 点击屏幕返pop回去 并跳到接单页面
-(void)sureSendViewdidRemove {
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"selectindex" object:@{@"index":@(0)}];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
